#include "RingQueue.hpp"
#include <pthread.h>
#include <unistd.h>
#include "Task.hpp"
void *Productor(void *args)
{
    //sleep(5);
    RingQueue<Task> *rq = static_cast<RingQueue<Task> *>(args);
    // int cnt = 100;
    while (true)
    {
        int data1 = rand() % 10;
        usleep(rand() % 123);
        int data2 = rand() % 10;
        char oper = opers[rand() % opers.size()];

        Task t(data1, data2, oper);
        rq->Push(t);
        std::string str = t.print();
        std::cout << "productor data " << str << std::endl;
        // rq->push
        // rq->Push(cnt);
        // std::cout << "product data " << cnt << std::endl;
        // cnt--;
        //    sleep(1);
    }
    // sleep(1);
}

void *Consumer(void *args)
{
    RingQueue<Task> *rq = static_cast<RingQueue<Task> *>(args);
    while (true)
    {
        sleep(1);
        Task t;
        rq->pop(&t);
        t.Run();
        std::cout << "consumer data " << t.printresult() << std::endl;
        //sleep(1);
        // rq->pop
        // int data = 0;
        // rq->pop(&data);
        // std::cout << "Consumer done " << data << std::endl;
    }
}

int main()
{
    //单生产 单消费
    srand(uint64_t(time(nullptr) ^ getpid() ^ pthread_self()));
    pthread_t c[2], p[3];
    RingQueue<Task> *rq = new RingQueue<Task>();
    pthread_create(&c[0], nullptr, Consumer, rq);
    pthread_create(&c[1], nullptr, Consumer, rq);

    pthread_create(&p[0], nullptr, Productor, rq);
    pthread_create(&p[1], nullptr, Productor, rq);
    pthread_create(&p[2], nullptr, Productor, rq);

    pthread_join(c[0], nullptr);
    pthread_join(c[0], nullptr);

    pthread_join(p[0], nullptr);
    pthread_join(p[1], nullptr);
    pthread_join(p[2], nullptr);

    return 0;
}